<!--
 * @Author: your name
 * @Date: 2020-12-21 09:05:43
 * @LastEditTime: 2020-12-21 10:55:37
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \intelligent-agriculture-identification-management-system_-ui\src\pages\file\DatasetFile.vue
-->
<template>
  <div class=''>
    <el-page-header @back="$router.go(-1)" content="上传图像"> </el-page-header>
    <div class="page_header">
      <div class="search_form">
        <el-form inline size="small">
          <el-form-item>
            <el-input></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" plain>搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="btns">
        <el-button type="primary" size="small" @click="toUploadHandler">上传</el-button>
      </div>
    </div>
    <el-table :data="files" size="small">
      <el-table-column label="图片" prop="fileId" align="center" width="100px">
        <template slot-scope="scope">
          <div style="width:80px;height:80px;border-radius:50%;overflow:hidden">
            <img style="width:100%" :src='"http://47.106.244.1:8888/group1/"+scope.row.fileId' alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片名称" prop="fileRealName" align="center"></el-table-column>
      <el-table-column label="图片介绍" prop="fileIntroduce" align="center"></el-table-column>
      <el-table-column label="操作" width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="toDeleteHandler(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 模态框 -->
    <el-dialog title="添加图片" :visible.sync="visible" width="40%" >
      <el-form size="small" :model="form" label-width="80px">
        <el-form-item label="图片">
          <el-upload
            action="http://47.106.244.1:7788/file/upload"
            list-type="picture-card"
            :file-list="fileList"
            :on-success="uploadSuccessHandler">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="图片标识">
          <el-input v-model="form.fileRealName"></el-input>
        </el-form-item>
        <el-form-item label="图片介绍">
          <el-input type="textarea" :rows="4" v-model="form.fileIntroduce"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitHandler">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>

<script>
import { get, post ,del} from '@/utils/request';
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     datasetId :0,
     files:[],
     fileList:[], // 上传过的图片
     visible:false,
     form:{}
   }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    toDeleteHandler(row){
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "http://8.131.68.179:9999/file_dataset_file/deleteById";
        del(url, { id: row.id }).then((resp) => {
          this.$message({ type: "success", message: resp.message });
          this.loadDatasetFiles();
        });
      });
    },
    submitHandler(){
      let url = "http://8.131.68.179:9999/file_dataset_file/saveOrUpdate"
      post(url,this.form).then(resp => {
        // 关闭模态框
        this.visible = false;
        // t提示
        this.$message({type:"success",message:resp.message})
        // 重载数据
        this.loadDatasetFiles();
      })
    },
    // 上传成功的处理函数
    uploadSuccessHandler(resp){
      if(resp.status == 200){
        // 将fileID填充到form中，待会保存的时候提交给后台
        let fileId = resp.data.id;
        this.form = {
          ...this.form,
          fileId
        }
      } else {
        this.$message({type:"error",message:resp.message})
      }
    },
    // 去上传
    toUploadHandler(){
      this.form = {datasetId : this.datasetId}
      this.fileList = []; // 防止下次上传的时候保留图片信息
      this.visible = true;
    },
    loadDatasetFiles(){
      let url = "http://8.131.68.179:9999/file_dataset_file/query";
      get(url,{datasetId:this.datasetId}).then(resp => {
        this.files = resp.data;
      })
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // this.$route.query用于获取路由参数，this.$router.push({path,query})
    this.datasetId = this.$route.query.datasetId;
    // 通过datasetId查找图片
    this.loadDatasetFiles();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>
.page_header {
  margin-bottom: 5px;
}
.page_header > .search_form {
  text-align: center;
}
.page_header > .btns {
  text-align: right;
}
</style>